package controller

import (
	"net"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/oschwald/geoip2-golang"
)

// PingTestController 测试
func PingTestController() gin.HandlerFunc {
	return func(c *gin.Context) {
		size := c.Query("size")
		db, err := geoip2.Open("GeoLite2-City.mmdb")
		if err != nil {
			panic(err)
		}
		defer db.Close()
		// 要查询的IP地址
		// 查询IP地址的信息
		record, err := db.City(net.ParseIP(c.ClientIP()))
		if err != nil {
			panic(err)
		}
		// 输出城市名字
		continent := record.Continent.Names["zh-CN"]      // 州
		country := record.Country.Names["zh-CN"]          // 国
		province := record.Subdivisions[0].Names["zh-CN"] // 省
		city := record.City.Names["zh-CN"]                // 市
		location := record.Location
		uid := uuid.NewSHA1(uuid.NameSpaceDNS, []byte(size))
		parseUUID, _ := uuid.Parse(uid.String())
		c.JSON(http.StatusOK, gin.H{
			"location": gin.H{
				"continent": continent,
				"country":   country,
				"province":  province,
				"city":      city,
				"location":  location,
			},
			"message":   "pong",
			"data":      uid,
			"parseUUID": parseUUID,
			"timestamp": time.Now(),
		})
	}
}
